import grails.converters.JSON
class CompanysnoldController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ companysnInstanceList: Companysn.list( params ), companysnInstanceTotal: Companysn.count() ]
    }

    def show = {
        def companysnInstance = Companysn.get( params.id )

        if(!companysnInstance) {
            flash.message = "Companysn not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ companysnInstance : companysnInstance ] }
    }

    def delete = {
        def companysnInstance = Companysn.get( params.id )
        if(companysnInstance) {
            companysnInstance.delete()
            flash.message = "Companysn ${params.id} deleted"
            redirect(action:list)
        }
        else {
            flash.message = "Companysn not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def companysnInstance = Companysn.get( params.id )

        if(!companysnInstance) {
            flash.message = "Companysn not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ companysnInstance : companysnInstance ]
        }
    }

    def update = {
        def companysnInstance = Companysn.get( params.id )
        if(companysnInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(companysnInstance.version > version) {
                    
                    companysnInstance.errors.rejectValue("version", "companysn.optimistic.locking.failure", "Another user has updated this Companysn while you were editing.")
                    render(view:'edit',model:[companysnInstance:companysnInstance])
                    return
                }
            }
            companysnInstance.properties = params
            if(!companysnInstance.hasErrors() && companysnInstance.save()) {
                flash.message = "Companysn ${params.id} updated"
                redirect(action:show,id:companysnInstance.id)
            }
            else {
                render(view:'edit',model:[companysnInstance:companysnInstance])
            }
        }
        else {
            flash.message = "Companysn not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def companysnInstance = new Companysn()
        companysnInstance.properties = params
        return ['companysnInstance':companysnInstance]
    }

    def save = {
        def companysnInstance = new Companysn(params)
        if(!companysnInstance.hasErrors() && companysnInstance.save()) {
            flash.message = "Companysn ${companysnInstance.id} created"
            redirect(action:show,id:companysnInstance.id)
        }
        else {
            render(view:'create',model:[companysnInstance:companysnInstance])
        }
    }
    def search={
        SearchCompanysnCommand  searchCmd ->
        def _max = params.limit?params.limit:10
        def _offset = params.start?params.start:0
        def criteria = Companysn.createCriteria() //创建了一个 Hibernate 的 Criteria 查询
        def c = Companysn.createCriteria()
        def branchCount = c.get {
            projections {
                count "id"
            }
            and {
                if(searchCmd.domain)
                like ('domain',"%${searchCmd.domain}%")
                if(searchCmd.callcenterDN)
                like ('callcenterDN',"%${searchCmd.callcenterDN}%")
                if(searchCmd.companyName)
                like ('companyName',"%${searchCmd.companyName}%")
                if(searchCmd.companySN)
                like ('companySN',"%${searchCmd.companySN}%")
                //if(searchCmd.direction)
                //eq ('direction',Integer.valueOf("${searchCmd.direction}"))
            }
        }
        def resultList = criteria{
            and {
                if(searchCmd.domain)
                like ('domain',"%${searchCmd.domain}%")
                if(searchCmd.callcenterDN)
                like ('callcenterDN',"%${searchCmd.callcenterDN}%")
                if(searchCmd.companyName)
                like ('companyName',"%${searchCmd.companyName}%")
                if(searchCmd.companySN)
                like ('companySN',"%${searchCmd.companySN}%")

                //如果 teamName 不为空，则与 team 表进行连接，并加入查询条件
                //if(searchCmd.teamName) {
                //team{
                //like ('teamName',"%${searchCmd.teamName}%" )
                //}
                //fetchMode('team', FetchMode.EAGER)
                //}
            }
            maxResults(Integer.valueOf(_max))
            order("id", "asc") //加入了排序的条件，以 id 字段进行升序查询
            firstResult(Integer.valueOf(_offset))
        }

        
        //flash.message=resultListsum;
        //render resultList as JSON
        render(contentType:"application/json"){
    totalCount(branchCount)
    csns{
      for(a in resultList){
        csn(id:a.id,
            companyname:a.companyName,
            callcenterdn:a.callcenterDN,
            companysn:a.companySN,
            domain:a.domain,
            //callcenterDN:a.companySN.callcenterDN,
            //msinum:a.msiinfos.size(),
            userid:a.userID
        )
      }
    }
  }
        //println(searchCmd.companyName);
        //render(view:"list" , model:[companysnInstanceList: resultList,companysnInstanceTotal: branchCount])
    }
}
